package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("页面传输的数据：{}",shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        //查询当前菜品或套餐是否已经在购物车之中，如果已经存在，则再次添加只需要增加数量
        //如果dishId不为null，说明目前在向购物车添加菜品
        if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //按以上条件查询
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        if(cartServiceOne!=null){
            //不为null,则在原来数量上+1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }
        return R.success(cartServiceOne);
    }
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }
    @PostMapping("/sub")
    public R<ShoppingCart> subtract(@RequestBody ShoppingCart shoppingCart){
        //查询数据库，查看当前物品数量，如果为1，则从数据库删除，如果大于1，则数量减一
        Long setmealId = shoppingCart.getSetmealId();
        //dishId为null，则删除
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        if(setmealId!=null){
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        Integer number = cartServiceOne.getNumber();
        if(number>1){
            cartServiceOne.setNumber(number-1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            cartServiceOne.setNumber(number-1);
            shoppingCartService.removeById(cartServiceOne);
        }
        return R.success(cartServiceOne);
    }

}
